//
//  MyPickerView.m
//  安好
//
//  Created by qinlongqing on 14/11/27.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "MyPickerView.h"

#define kSheetAccessoryViewHeight    35

@interface MyPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
    UIView *sheetAccessoryView;
    UIPickerView *myPickerView;
}
@end

@implementation MyPickerView

- (void)setFrame:(CGRect)frame{
    myPickerView.frame = CGRectMake(0, kSheetAccessoryViewHeight, frame.size.width, frame.size.height - kSheetAccessoryViewHeight);
    [super setFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupWithFrame:frame];
    }
    return self;
}

- (void)setupWithFrame:(CGRect)anFrame{
    self.backgroundColor = [UIColor whiteColor];
    [self createSheetAccessoryViewWithFrame:anFrame];
    [self createSheetViewWithFrame:anFrame];
}

- (void)createSheetAccessoryViewWithFrame:(CGRect)anFrame{
//    sheetAccessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, anFrame.size.width, kSheetAccessoryViewHeight)];
//    sheetAccessoryView.backgroundColor = [UIColor grayColor];
//    [self addSubview:sheetAccessoryView];
    
    UIButton *cancelBTN = [[UIButton alloc]initWithFrame:CGRectMake(20, 5, 50, kSheetAccessoryViewHeight - 10)];
    [cancelBTN addTarget:self action:@selector(cancelSelect) forControlEvents:UIControlEventTouchUpInside];
    [cancelBTN setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBTN setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:cancelBTN];
    
    UIButton *sureBTN = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth - 20 - 50, 5, 50, kSheetAccessoryViewHeight - 10)];
    [sureBTN addTarget:self action:@selector(sureSelect) forControlEvents:UIControlEventTouchUpInside];
    [sureBTN setTitle:@"确认" forState:UIControlStateNormal];
    [sureBTN setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:sureBTN];
}

- (void)createSheetViewWithFrame:(CGRect)anFrame{
    myPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, kSheetAccessoryViewHeight, anFrame.size.width, anFrame.size.height - kSheetAccessoryViewHeight)];
    myPickerView.delegate = self;
    myPickerView.dataSource = self;
    [self addSubview:myPickerView];
}

- (void)sureSelect{
    if (self.delegate && [self.delegate respondsToSelector:@selector(myPickerViewSureSelected:)]) {
        [self.delegate myPickerViewSureSelected:self];
    }
}

- (void)cancelSelect{
    if (self.delegate && [self.delegate respondsToSelector:@selector(myPickerViewCancelSelected:)]) {
        [self.delegate myPickerViewCancelSelected:self];
    }
}

- (void)reloadAllComponents{
    [myPickerView reloadAllComponents];
}

- (void)reloadComponent:(NSInteger)component{
    [myPickerView reloadComponent:component];
}

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated{
    [myPickerView selectRow:row inComponent:component animated:animated];
}

- (NSInteger)selectedRowInComponent:(NSInteger)component{
    return [myPickerView selectedRowInComponent:component];
}


#pragma - mark UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if (self.delegate && [self.delegate respondsToSelector:@selector(numberOfComponentsInMyPickerView:)]) {
        return [self.delegate numberOfComponentsInMyPickerView:self];
    }
    return 0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (self.delegate && [self.delegate respondsToSelector:@selector(myPickerView:numberOfRowsInComponent:)]) {
        return [self.delegate myPickerView:self numberOfRowsInComponent:component];
    }
    return 0;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    if (self.delegate && [self.delegate respondsToSelector:@selector(myPickerView:rowHeightForComponent:)]) {
        return [self.delegate myPickerView:self rowHeightForComponent:component];
    }
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (self.delegate && [self.delegate respondsToSelector:@selector(myPickerView:titleForRow:forComponent:)]) {
        return [self.delegate myPickerView:self titleForRow:row forComponent:component];
    }
    return nil;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (self.delegate && [self.delegate respondsToSelector:@selector(myPickerView:didSelectRow:inComponent:)]) {
        [self.delegate myPickerView:self didSelectRow:row inComponent:component];
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
